package router

import (
	"admin-system-go/model"
	"admin-system-go/model/dto"
	"admin-system-go/service"
	"github.com/gin-gonic/gin"
	"net/http"
)

type UserGroupRouter struct {
	userGroupService service.UserGroupService
}

func NewUserGroupRouter() UserGroupRouter {
	return UserGroupRouter{userGroupService: service.GetUserGroupService()}
}

func (UserGroupRouter UserGroupRouter) Register(r *gin.Engine) {
	r.GET(apiVersion+"/user/group/list", UserGroupRouter.QueryUserGroup())
	r.POST(apiVersion+"/user/group/create", UserGroupRouter.createUserGroup())
	r.POST(apiVersion+"/user/group/update", UserGroupRouter.updateUserGroup())
	r.POST(apiVersion+"/user/group/delete", UserGroupRouter.deleteUserGroup())
}

func (UserGroupRouter UserGroupRouter) QueryUserGroup() gin.HandlerFunc {
	return func(context *gin.Context) {
		var userGroupQueryDTO dto.UserGroupQueryDTO
		err := context.ShouldBind(&userGroupQueryDTO)
		if err != nil {
			context.JSON(http.StatusOK, model.ParamErrorModel)
			return
		}
		context.JSON(http.StatusOK, UserGroupRouter.userGroupService.QueryUserGroup(userGroupQueryDTO))
	}
}

func (UserGroupRouter UserGroupRouter) createUserGroup() gin.HandlerFunc {
	return func(context *gin.Context) {
		var userGroupBaseDTO dto.UserGroupBaseDTO
		err := context.ShouldBind(&userGroupBaseDTO)
		if err != nil {
			context.JSON(http.StatusOK, model.ParamErrorModel)
			return
		}
		context.JSON(http.StatusOK, UserGroupRouter.userGroupService.CreateUserGroup(userGroupBaseDTO))
	}
}

func (UserGroupRouter UserGroupRouter) deleteUserGroup() gin.HandlerFunc {
	return func(context *gin.Context) {
		var userGroupDelDTO dto.UserGroupDelDTO
		err := context.ShouldBind(&userGroupDelDTO)
		if err != nil {
			context.JSON(http.StatusOK, model.ParamErrorModel)
			return
		}
		context.JSON(http.StatusOK, UserGroupRouter.userGroupService.DelUserGroup(userGroupDelDTO))
	}
}

func (UserGroupRouter UserGroupRouter) updateUserGroup() gin.HandlerFunc {
	return func(context *gin.Context) {
		var userGroupUpdateDTO dto.UserGroupUpdateDTO
		err := context.ShouldBind(&userGroupUpdateDTO)
		if err != nil {
			context.JSON(http.StatusOK, model.ParamErrorModel)
			return
		}
		context.JSON(http.StatusOK, UserGroupRouter.userGroupService.UpdateUserGroup(userGroupUpdateDTO))
	}
}
